package com.mlethe.android.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Layout;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mlethe.android.demo.banner.MainBannerActivity;
import com.mlethe.android.demo.clearedit.MainClearEditActivity;
import com.mlethe.android.demo.dialog.MainDialogActivity;
import com.mlethe.android.demo.fingerprint.MainFingerprintActivity;
import com.mlethe.android.demo.gesturelock.MainGestureActivity;
import com.mlethe.android.demo.keyboard.MainKeyboardActivity;
import com.mlethe.android.demo.loadingindicator.MainLoadingIndicatorActivity;
import com.mlethe.android.demo.nicespinner.MainNiceSpinnerActivity;
import com.mlethe.android.demo.party.MainPartyActivity;
import com.mlethe.android.demo.plane.PlaneSeatActivity;
import com.mlethe.android.demo.progress.MainProgressActivity;
import com.mlethe.android.demo.qrcode.MainQRCodeActivity;
import com.mlethe.android.demo.receiver.MyReceiver;
import com.mlethe.android.demo.recyclerview.adapter.MainRecyclerAdapterActivity;
import com.mlethe.android.demo.ripple.MainRippleActivity;
import com.mlethe.android.demo.roundimage.MainRoundImageActivity;
import com.mlethe.android.demo.seattable.activity.MovieActivity;
import com.mlethe.android.demo.utils.SensorManagerHelper;
import com.mlethe.android.demo.verifycode.MainVerifyCodeActivity;
import com.mlethe.android.demo.verticaltextview.VerticalTextViewActivity;
import com.mlethe.android.demo.wheelview.MainWheelViewActivity;
import com.mlethe.library.navigation.DefaultNavigationBar;
import com.mlethe.library.utils.DataTool;
import com.mlethe.library.utils.RegTool;
import com.mlethe.library.utils.app.AppTool;
import com.mlethe.library.utils.app.ShortcutIconUtil;
import com.mlethe.library.utils.app.StatusBarUtil;
import com.mlethe.library.utils.device.DeviceUtil;
import com.mlethe.library.utils.view.TextTool;

import java.util.concurrent.atomic.AtomicInteger;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView contentTv = findViewById(R.id.contentTv);
        findViewById(R.id.banner_btn).setOnClickListener(this);
        findViewById(R.id.round_image_btn).setOnClickListener(this);
        findViewById(R.id.clear_edit_btn).setOnClickListener(this);
        findViewById(R.id.dialog_btn).setOnClickListener(this);
        findViewById(R.id.gesture_btn).setOnClickListener(this);
        findViewById(R.id.recycler_adapter_btn).setOnClickListener(this);
        findViewById(R.id.party_btn).setOnClickListener(this);
        findViewById(R.id.wheelview_btn).setOnClickListener(this);
        findViewById(R.id.verifycode_btn).setOnClickListener(this);
        findViewById(R.id.seattable_btn).setOnClickListener(this);
        findViewById(R.id.keyboard_btn).setOnClickListener(this);
        findViewById(R.id.nice_spinner_btn).setOnClickListener(this);
        findViewById(R.id.progress_btn).setOnClickListener(this);
        findViewById(R.id.ripple_btn).setOnClickListener(this);
        findViewById(R.id.fingerprint_btn).setOnClickListener(this);
        findViewById(R.id.shortcut_btn).setOnClickListener(this);
        findViewById(R.id.loading_indicator_btn).setOnClickListener(this);
        findViewById(R.id.scan_btn).setOnClickListener(this);
        findViewById(R.id.plane_seat_btn).setOnClickListener(this);
        findViewById(R.id.vertical_text_btn).setOnClickListener(this);
        StatusBarUtil.transparent(this);
//        StatusBarUtil.textColor(this, true);
        int padding = (int) getResources().getDimension(R.dimen.dp_5);
        DefaultNavigationBar navigationBar = new DefaultNavigationBar.Builder(this)
                .setTitle("测试")
                .setTitleColor("#EFA859")
                .setLeftIcon(R.mipmap.ic_back_circle)
                .setOverflowIcon(R.drawable.ic_more)
                .setOverflowIconColor("#EFA859")
                .setRightIcon(R.mipmap.ic_launcher)
                .setMenuId(R.menu.toolbar_menu)
                .setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int id = item.getItemId();
                        if (id == R.id.action_search) {
                            Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    }
                })
                .setRightIconScaleType(ImageView.ScaleType.FIT_XY)
                .setRightIconWidth((int) getResources().getDimension(R.dimen.dp_20))
                .setRightIconPadding(padding)
                .setRightText("注册")
                .isPadding(true)
                .setStatusBarTransparent(true)
                .create();
        Log.e("TAG", "onCreate: " + RegTool.isIDCard("134676436565563434"));
        String string = DataTool.bytes2HexString(new byte[]{});
        Log.e("TAG", "onCreate: " + string);
        contentTv.setMovementMethod(LinkMovementMethod.getInstance());
        TextTool.getBuilder(this, "今日免费采风次数已用完\n可花")
                .setAlign(Layout.Alignment.ALIGN_CENTER)
                .appendResourceId(R.mipmap.ic_shop_theme_buy)
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View view) {
                        Toast.makeText(MainActivity.this, "点击喵饼", Toast.LENGTH_SHORT).show();
                    }
                })
                .append("30")
                .setAlign(Layout.Alignment.ALIGN_OPPOSITE)
                .setForegroundColor(Color.parseColor("#F6657D"))
                .append("增加1次采风机会")
                .setAlign(Layout.Alignment.ALIGN_CENTER)
                .into(contentTv);
        AtomicInteger count = new AtomicInteger();
        SensorManagerHelper sensorManagerHelper = new SensorManagerHelper(this);
        sensorManagerHelper.setOnShakeListener(() -> {
            count.incrementAndGet();
            Log.e("yym", "onShake: " + count);
        }).start();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.banner_btn) {
            Intent intent = new Intent(this, MainBannerActivity.class);
            startActivity(intent);
        } else if (id == R.id.round_image_btn) {
            Intent intent = new Intent(this, MainRoundImageActivity.class);
            startActivity(intent);
        } else if (id == R.id.clear_edit_btn) {
            Intent intent = new Intent(this, MainClearEditActivity.class);
            startActivity(intent);
        } else if (id == R.id.dialog_btn) {
            Intent intent = new Intent(this, MainDialogActivity.class);
            startActivity(intent);
        } else if (id == R.id.gesture_btn) {
            Intent intent = new Intent(this, MainGestureActivity.class);
            startActivity(intent);
        } else if (id == R.id.recycler_adapter_btn) {
            Intent intent = new Intent(this, MainRecyclerAdapterActivity.class);
            startActivity(intent);
        } else if (id == R.id.party_btn) {
            Intent intent = new Intent(this, MainPartyActivity.class);
            startActivity(intent);
        } else if (id == R.id.wheelview_btn) {
            Intent intent = new Intent(this, MainWheelViewActivity.class);
            startActivity(intent);
        } else if (id == R.id.verifycode_btn) {
            Intent intent = new Intent(this, MainVerifyCodeActivity.class);
            startActivity(intent);
        } else if (id == R.id.seattable_btn) {
            Intent intent = new Intent(this, MovieActivity.class);
            startActivity(intent);
        } else if (id == R.id.keyboard_btn) {
            Intent intent = new Intent(this, MainKeyboardActivity.class);
            startActivity(intent);
        } else if (id == R.id.nice_spinner_btn) {
            Intent intent = new Intent(this, MainNiceSpinnerActivity.class);
            startActivity(intent);
        } else if (id == R.id.progress_btn) {
            Intent intent = new Intent(this, MainProgressActivity.class);
            startActivity(intent);
        } else if (id == R.id.ripple_btn) {
            Intent intent = new Intent(this, MainRippleActivity.class);
            startActivity(intent);
        } else if (id == R.id.fingerprint_btn) {
            Intent intent = new Intent(this, MainFingerprintActivity.class);
            startActivity(intent);
        } else if (id == R.id.shortcut_btn) {
            ShortcutIconUtil.add(this, MainRippleActivity.class, MyReceiver.class, "快捷方式", R.mipmap.ic_test_img);
        } else if (id == R.id.loading_indicator_btn) {
            Intent intent = new Intent(this, MainLoadingIndicatorActivity.class);
            startActivity(intent);
        } else if (id == R.id.scan_btn) {
            Intent intent = new Intent(this, MainQRCodeActivity.class);
            startActivity(intent);
        } else if (id == R.id.plane_seat_btn) {
            Intent intent = new Intent(this, PlaneSeatActivity.class);
            startActivity(intent);
        } else if (id == R.id.vertical_text_btn) {
            Intent intent = new Intent(this, VerticalTextViewActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        String s = DeviceUtil.getPesudoUniqueId() + "\nWIFI MAC地址：" + DeviceUtil.getMacAddress(this) + "\n蓝牙MAC地址：" + DeviceUtil.getBTMACAddress() + "\n唯一标识：" + DeviceUtil.getUniqueId(this) + DeviceUtil.getDeviceInfo();
        Log.e("yym", "onResume: " + s);
    }

    @Override
    protected void onPause() {
        super.onPause();
        new Handler().postDelayed(() -> {
            if (!AppTool.isOnForeground(this)) {
                Toast.makeText(this, "APP进入后台运行", Toast.LENGTH_SHORT).show();
            }
        }, 200);
    }
}
